define(function(require, exports) {
	var c1 = {};

	c1 = {
	  form : {
	    joinCheckboxs : function(options) {
		    if (options.mainSelector == undefined || options.childSelector == undefined) {
			    return;
		    }

		    options = $.extend({
		      closestTagName : 'tr',
		      closestTagCssName : 'success'
		    }, options);

		    $(document).on('click', options.mainSelector, function(event) {
			    var $this = $(this);

			    var checked = $this.is(':checked');

			    if (checked) {
				    $(options.childSelector).prop({
					    'checked' : true
				    }).closest(event.data.options.closestTagName).addClass(event.data.options.closestTagCssName);
			    } else {
				    $(options.childSelector).prop({
					    'checked' : false
				    }).closest(options.closestTagName).removeClass(options.closestTagCssName);
			    }
		    });

		    $(document).on('click', options.childSelector, function(event) {
			    var $this = $(this);

			    var checked = $this.is(':checked');

			    var childSelectorCheckedSize = $(options.childSelector).filter(':checked').size();

			    var childSelectorSize = $(options.childSelector).size();

			    if (checked) {
				    $(options.mainSelector).prop({
					    'checked' : (childSelectorSize == childSelectorCheckedSize)
				    });

				    $this.closest(options.closestTagName).addClass(options.closestTagCssName);
			    } else {
				    $(options.mainSelector).prop({
					    'checked' : false
				    });

				    $this.closest(options.closestTagName).removeClass(options.closestTagCssName);
			    }
		    });
	    },
	    serializeJson : function(selector) {
		    return $(selector).serializeArray();
	    },
	    pagination : function(options) {
		    if (options.paginationSelector == undefined || options.paginationNoSelector == undefined) {
			    return;
		    }

		    if (!$.isFunction(options.loadPagination)) {
			    return;
		    }

		    $(document).on("click", options.paginationSelector + ' a[href^="#no_"]', function() {
			    var val = $(this).attr('href');

			    val = val.replace('#no_', '');

			    $(options.paginationNoSelector).val(val);

			    options.loadPagination();
		    });
	    },
	    editor : function(options) {
		    options = $.extend({}, options);

		    require.async([ 'ckeditor', 'ckeditor-jquery' ], function() {
			    $(options.selector).ckeditor(options);
		    });
	    },
	    upload : function(options) {
		    options = $.extend({}, options);
		    
		    require.async([ 'jquery-ui', 'jquery-iframe-transport','fileupload-js'  ], function() {
			    $(options.selector).fileupload({
			      url : options.url,
			      dataType : 'json',
			      done : function(e, data) {
			      },
			      progressall : function(e, data) {
				      var progress = parseInt(data.loaded / data.total * 100, 10);

				      $('#progress .progress-bar').css('width', progress + '%');
			      }
			    }).prop('disabled', !$.support.fileInput).parent().addClass($.support.fileInput ? undefined : 'disabled');
		    });
	    },
	    insertAtCursor : function(options) {
		    var $this = $(options.selector);

		    if ($this.size() < 1) {
			    return;
		    }

		    var $t = $this.get(0);

		    if (document.selection) {
			    $t.focus();

			    sel = document.selection.createRange();

			    sel.text = options.value;

			    $t.focus();

			    return;
		    }

		    if ($t.selectionStart || $t.selectionStart == '0') {
			    var startPos = $t.selectionStart;
			    var endPos = $t.selectionEnd;
			    var scrollTop = $t.scrollTop;

			    $t.value = $t.value.substring(0, startPos) + options.value + $t.value.substring(endPos, $t.value.length);

			    $t.focus();

			    $t.selectionStart = startPos + options.value.length;
			    $t.selectionEnd = startPos + options.value.length;
			    $t.scrollTop = scrollTop;

			    return;
		    }

		    $t.value += options.value;

		    $t.focus();
	    }
	  },
	  message : {
	    progress : function(onOff) {
		    var selector = '.c1-modal-progress';

		    var $c1Modal = $(selector);

		    var $progress = $c1Modal.find('.progress-bar');

		    $progress.css("width", "0%").animate({
			    "width" : "100%"
		    }, 8000);

		    if (onOff) {
			    $progress.stop().css("width", "100%");

			    $c1Modal.modal('hide');

			    return;
		    }

		    $c1Modal.modal('show');
	    },
	    alert : function(options) {
		    options = $.extend({
			    selector : '.c1-modal-alert'
		    }, options);

		    var $c1Modal = $(options.selector);

		    if (options.title) {
			    $c1Modal.find('.modal-title').html(options.title);
		    }

		    if (options.message) {
			    $c1Modal.find('.modal-body').html(options.message);
		    }

		    $c1Modal.data('options', options);

		    if (!$c1Modal.data('init')) {
			    $c1Modal.on('hidden.bs.modal', function(e) {
				    var options = $c1Modal.data('options');

				    if ($.isFunction(options.fn)) {
					    options.fn(e);
				    }
			    });

			    $c1Modal.data('init', true);
		    }

		    $c1Modal.modal('show');
	    },
	    confirm : function(options) {
		    options = $.extend({
			    selector : '.c1-modal-confirm'
		    }, options);

		    var $c1Modal = $(options.selector);

		    if (options.title) {
			    $c1Modal.find('.modal-title').html(options.title);
		    }

		    if (options.message) {
			    $c1Modal.find('.modal-body').html(options.message);
		    }

		    $c1Modal.data('options', options);

		    $c1Modal.data('r', false);

		    if (!$c1Modal.data('init')) {
			    $c1Modal.on('hidden.bs.modal', function(e) {
				    var options = $c1Modal.data('options');

				    if ($.isFunction(options.fn)) {
					    options.fn($c1Modal.data('r'));
				    }
			    });

			    $c1Modal.find('.c1-btn-confirm').click(function() {
				    $c1Modal.data('r', true);
			    });

			    $c1Modal.find('.c1-btn-cancel').click(function() {
				    $c1Modal.data('r', false);
			    });

			    $c1Modal.data('init', true);
		    }

		    $c1Modal.modal('show');
	    }
	  }
	};

	return c1;
});